class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size();
        if(n == 0) return 0;
        unordered_map<int,int> hash;
        vector<int> dp(n,1);
        int Max = 1;
        
        for(int i = 0;i < n;i++){
            int x = arr[i] - difference;
            if(hash.count(x)){
                dp[i] = dp[hash[x]] + 1;
                Max = max(dp[i],Max);
            }
            hash[arr[i]] = i;
        }
        return Max;
    }
};
